/*
 * G2P_NGram.cpp
 *
 *  Created on: Nov 24, 2012
 *      Author: bannok
 */
#include "g2p_tone_gram.h"

Darts::DoubleArray tone_bi_gram_da;
Darts::DoubleArray tone_pattern_da;
tone_bi_struct ** tone_prob_st;
tone_struct ** tone_dtone ;
int tone_st_len=0;

/* load prob file (bin)*/
int tone_ngram_prob_load_bin(const char* input){
	tone_prob_st = (tone_bi_struct **)calloc(TONE_N_GRAM_PROB_MAX,sizeof(tone_bi_struct *));
	FILE * fp = fopen(input,"r");
	if(fp==NULL) return 0;
	while(!feof(fp)){
		tone_bi_struct st ;
		fread(&st,sizeof(tone_bi_struct),1,fp);
		tone_bi_struct * new_st = (tone_bi_struct *)malloc(sizeof(tone_bi_struct));
		new_st->p = st.p;
		new_st->bo = st.bo;
		tone_prob_st[tone_st_len] = new_st;
		tone_st_len++;
	}
	/*
	printf("\ni = %d",prob_st_len);
	for(int i=0;i<prob_st_len;i++){
		printf("%d\t%.4f\t%.4f",i,prob_st[i]->p,prob_st[i]->bo);
		printf("\n");
	}*/
	return 1;
}
/* load dict of prob*/
int tone_ngram_prob_load_dict(char * fn){
	std::string index = fn;
		if (tone_bi_gram_da.open(index.c_str())) {
			std::cerr << "Error: cannot open " << index << std::endl;
			return -1; // Load Error.
		}
	return 0;
}
/* load dict of tone */
int tone_ngram_pattern_load_dict(char * fn){
	std::string index = fn;
		if (tone_pattern_da.open(index.c_str())) {
			std::cerr << "Error: cannot open " << index << std::endl;
			return -1; // Load Error.
		}
	return 0;
}
int tone_ngram_pattern_seach(char * st){
	int re =-1 ;
	tone_pattern_da.exactMatchSearch(st,re);
	//printf("index = %d",re);
	if(re!=-1)
		return tone_dtone[re]->tone;
	else
		return -1;
}
int tone_ngram_seach(char * st){
	int re ;
	tone_bi_gram_da.exactMatchSearch(st,re);
	return re;
}
float tone_ngram_calculate(char * st1,char * st2){
//	p(wd2|wd1)= if(bigram exists) p_2(wd1,wd2)
//	            else              bo_wt_1(wd1)*p_1(wd2)

	char tmp[50];
	sprintf(tmp,"%s%s",st1,st2);
	int r1 = tone_ngram_seach(tmp);

	if(r1!=-1){
		return tone_prob_st[r1]->p;
	}else{
		float p1,p2;
		int r2 = tone_ngram_seach(st1);
		int r3 = tone_ngram_seach(st2);

		if(r2!=-1){
			p1 = tone_prob_st[r2]->bo;
		}else{
			r2 = tone_ngram_seach("U");
			p1 = tone_prob_st[r2]->bo;
		}
		if(r3!=-1){
			p2 = tone_prob_st[r3]->p;
		}else{
			r3 = tone_ngram_seach("U");
			p2 = tone_prob_st[r3]->bo;
		}
		return p1+p2;

	}

}
int tone_ngram_pattern_load_bin(char * input){
		tone_dtone = (tone_struct **)calloc(TONE_N_GRAM_DTONE_MAX,sizeof(tone_struct *));
		FILE * fp = fopen(input,"r");
		int i =0;
		while(!feof(fp)){
			tone_struct st ;
			fread(&st,sizeof(tone_struct),1,fp);
			tone_dtone[i] = (tone_struct *)malloc(sizeof(tone_struct)) ;
			tone_dtone[i]->tone = st.tone;
			i++;
			//printf("\n%d. %d",i,tone_dtone[i-1]->tone);
		}

		fclose(fp);
		//printf("\ntone_load_OK");
}

tone_node * tone_ngram_create_node(){
	tone_node * ntone = (tone_node *)malloc(sizeof(tone_node));
	ntone->child = NULL;
	ntone->parent = NULL;
	ntone->tone_count = 0;
	ntone->select_tone = 0;
	ntone->tone_prob_size = 0;
	ntone->max_prob = -99999;
	ntone->st_n = 0;
	return ntone;
}
int tone_ngram_free_node(tone_node * ntone ){
	printf("\n%x",ntone);
	if(ntone==NULL){
		return -1;
	}else{
		tone_node * curr = ntone;
		tone_ngram_free_node(curr->child);
		free(curr);
	}
	return 0;
}



